package com.csun.dsms.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.csun.dsms.comment.core.support.BaseService;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.mapper.HgFactoryDataMapper;
import com.csun.dsms.mapper.HgFinancialDataMapper;
import com.csun.dsms.model.huang.*;
import com.csun.dsms.service.HgFactoryDataService;
import com.csun.dsms.service.HgFinancialDataService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class HgFinancialDataServiceImpl extends BaseService<HgFinancialData>
        implements HgFinancialDataService {

    @Resource
    private HgFinancialDataMapper hgFinancialDataMapper;
    
    @Override
    public List<HgFinancialData> queryDataList(HgQueryDto hgQueryDto) {
        PageHelper.startPage(hgQueryDto.getPageNum(),hgQueryDto.getPageSize());
        return hgFinancialDataMapper.queryListWithPage(hgQueryDto);
    }

    @Override
    public List<HgYearMonthVo> groupByMonth(HgQueryDto hgQueryDto) {
        return hgFinancialDataMapper.groupByMonth(hgQueryDto);
    }

    @Override
    public Wrapper importFinancialData(MultipartFile file) {
        try {
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
            List<Map<String, Object>> ExcelList = reader.readAll();
            if (CollectionUtil.isEmpty(ExcelList)) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "Excel不能为空！");
            }
            for (Map<String, Object> excelMap : ExcelList) {
                HgFinancialData hgFinancialData = new HgFinancialData();
                hgFinancialData.setStatisticalYear(Convert.toInt(excelMap.get("统计年份")));
                hgFinancialData.setStatisticalMonth(Convert.toInt(excelMap.get("统计月")));
                hgFinancialData.setArea(Convert.toStr(excelMap.get("数据的区域")));
                hgFinancialData.setAreaLevel(Convert.toInt(excelMap.get("是市的还是区的")));
                hgFinancialData.setIndustryName(Convert.toStr(excelMap.get("行业名称")));
                hgFinancialData.setCentralProvBudgetAmt(Convert.toDouble(excelMap.get("中央省预算金额")));
                hgFinancialData.setCentralProvBudgetTarget(Convert.toDouble(excelMap.get("中央省预算目标")));
                hgFinancialData.setLlscBondAmt(Convert.toDouble(excelMap.get("两重两新超长期特别国债金额")));
                hgFinancialData.setLlscBondTarget(Convert.toDouble(excelMap.get("两重两新超长期特别国债目标")));
                hgFinancialData.setLocalBondAmt(Convert.toDouble(excelMap.get("本地债券金额")));
                hgFinancialData.setLocalBondTarget(Convert.toDouble(excelMap.get("本地债券目标")));
                hgFinancialData.setOtherFundAmt(Convert.toDouble(excelMap.get("其他基金金额")));
                hgFinancialData.setOtherFundTarget(Convert.toDouble(excelMap.get("其他基金目标")));
                hgFinancialData.setTotalFundAmt(Convert.toDouble(excelMap.get("总基金金额")));
                hgFinancialData.setTotalFundTarget(Convert.toDouble(excelMap.get("总基金目标")));
                hgFinancialData.setEntryDepartment(Convert.toStr(excelMap.get("上传的部门")));
                hgFinancialData.setCreator(Convert.toStr(excelMap.get("上传人")));
                hgFinancialData.setApproved(Convert.toInt(excelMap.get("是否审核")));
                hgFinancialData.setApproval(Convert.toStr(excelMap.get("审核人")));
                hgFinancialData.setCreatedTime(new Date());
                hgFinancialDataMapper.insert(hgFinancialData);
            }
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, "导入争资争项清单数据成功！");
        } catch (Exception e) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
        }
    }
}
